﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace VSS_AHT.Helpers
{
    public class ConfigProcessing
    {
        private static bool CreateConfigurationFile(string connectionString)
        {
            bool result = false;
            try
            {
                // Get the current configuration file.
                System.Configuration.Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

                // Create and add an entry to appSettings section.

                string conStringname = "ConnectionString";
                connectionString = Security.Encrypt(connectionString);
                // string providerName = "System.Data.SqlClient";

                //ConnectionStringSettings connStrSettings = new ConnectionStringSettings();
                //connStrSettings.Name = conStringname;
                //connStrSettings.ConnectionString = conString;
                //connStrSettings.ProviderName = providerName;

                config.ConnectionStrings.ConnectionStrings[conStringname].ConnectionString = connectionString;

                config.Save(ConfigurationSaveMode.Full);

                ConfigurationManager.RefreshSection("connectionStrings");

                result = true;
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine("CreateConfigurationFile: {0}", err.ToString());
            }
            return result;
        }

        // Show how to create an instance of the Configuration class
        // that represents this application configuration file.  
        public static bool CreateConfigurationFile(string server, string database)
        {
            string conString = @"Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=True;";
            return CreateConfigurationFile(conString);
        }

        // Show how to create an instance of the Configuration class
        // that represents this application configuration file.  
        public static bool CreateConfigurationFile(string server, string database, string userid, string password)
        {
            string conString = @"Data Source=" + server + ";Initial Catalog=" + database + ";User ID=" + userid + ";Password=" + password + ";";
            return CreateConfigurationFile(conString);
        }

        // Show how to create an instance of the Configuration class
        // that represents this application configuration file.  
        public static void GetConnectionString(ref string server, ref string database, ref string userid, ref string password, ref bool local)
        {
            try
            {
                // Get the current configuration file.
                System.Configuration.Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

                // Create and add an entry to appSettings section.

                string conStringname = "ConnectionString";
                //string conString = config.ConnectionStrings.ConnectionStrings[conStringname].ConnectionString;
                string conString = Security.Decrypt(config.ConnectionStrings.ConnectionStrings[conStringname].ConnectionString);

                string[] parse = conString.ToUpper().Split(';');
                foreach (var item in parse)
                {
                    if (item.StartsWith("DATA SOURCE"))
                        server = item.Split('=')[1];
                    else if (item.StartsWith("INITIAL CATALOG"))
                        database = item.Split('=')[1];
                    else if (item.StartsWith("USER ID"))
                        userid = item.Split('=')[1];
                    else if (item.StartsWith("PASSWORD"))
                        password = item.Split('=')[1];
                    else if (item.StartsWith("INTEGRATED SECURITY"))
                        local = true;
                }

            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine("GetConnectionString: {0}", err.ToString());
            }

        }
    }
}
